﻿/*
COPYRIGHT (C) 2011-2013 Raynald Messié

THIS FILE IS PART OF SCRUMPILOT.

SCRUMPILOT IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUMPILOT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUMPILOT. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System.Collections.Generic;
using System.Linq;
using ScrumPilot.Domain.Models;
using ScrumPilot.TFSHelpers;

namespace ScrumPilot.Domain.BPL
{
    public class KanbanServices : ProjectAndKanbanServices
    {
        public KanbanServices(TFSServices tfs) : base(tfs) { }

        public void GetAllOtherIterations(out List<Project> selectedProject, out List<Project> unselectedProject)
        {
            selectedProject = new List<Project>();
            unselectedProject = (this.FollowedProject.Select(project => this.GetProject(project, false))).ToList();
            unselectedProject.RemoveAll(p => !p.Iterations.Any());

            using (var unitOfWork = new UnitOfWork())
            {
                var sp = unitOfWork.KanbanIterationRepository.All().ToArray();

                foreach (var spj in sp)
                {
                    var slprj = this.GetProject(spj.projectid);
                    var slIte = slprj.Iterations.First(p => p.SID == spj.iterationid);
                    slprj.Iterations = new[] { slIte };
                    selectedProject.Add(slprj);

                    foreach (var project in unselectedProject)
                    {
                        var ites = project.Iterations as List<Iteration>;
                        if (ites != null)
                        {
                            ites.RemoveAll(p => p.ID == spj.iterationpath);
                            project.Iterations = ites.OrderByDescending(o => o.ID).ToList();
                        }
                    }
                }
            }
            unselectedProject.RemoveAll(p => !p.Iterations.Any());
            unselectedProject = unselectedProject.OrderBy(o => o.Name).ToList();
        }

        public void UpdateKanbanIteration(int way, int iterationId, int projectId)
        {
            switch (way)
            {
                case 1: // remove from selected iteration

                    using (var unitOfWork = new UnitOfWork())
                    {
                        unitOfWork.KanbanIterationRepository.Delete(p => p.iterationid == iterationId && p.projectid == projectId);
                        unitOfWork.Save();
                    }
                    break;
                case 0:  // add this iteration in selected Iteration
                    var project = this.GetProject(projectId);
                    var iterationPath = project.Iterations.First(p => p.SID == iterationId).ID;
                    using (var unitOfWork = new UnitOfWork())
                    {
                        unitOfWork.KanbanIterationRepository.Add(new kanbaniterationselected
                                                                     {
                                                                         iterationid = iterationId,
                                                                         iterationpath = iterationPath,
                                                                         projectid = projectId
                                                                     });
                        unitOfWork.Save();
                    }
                    break;
            }
        }
    }
}
